﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAnWeb.DAO;
using DoAnWeb.DTO;
using DoAnWeb.BUS;

namespace DoAnWeb
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        string idMenu = "";
        TaiKhoanDTO u = new TaiKhoanDTO();
        string DuongDan = "~/Default.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDMenu"] == null)
                Session["IDMenu"] = "lbtnTrangChu";

            XuLy_Menu();
            DuongDan = Server.HtmlEncode(Request.RawUrl);
            if (!IsPostBack)
            {
                TaiKhoanDAO DAO = new TaiKhoanDAO();
                if (CurrentContext.IsLogged)
                {
                    if (CurrentContext.TaiKhoanDTO == null)
                    {
                        CurrentContext.TaiKhoanDTO = DAO.GetPassword(CurrentContext.UserName);
                    }
                    if (DAO.GetKind(CurrentContext.UserName) == "LTK1")
                    {
                        pnLogin.Visible = false;
                        lkCart.Visible = false;
                        hlkDoanhNghiep.Visible = false;
                    }
                    else if (DAO.GetKind(CurrentContext.UserName) == "LTK3")
                    {
                        pnLogin.Visible = false;
                        hlkQuanTri.Visible = false;
                        lkCart.Visible = false;
                    }
                    else
                    {
                        pnLogin.Visible = false;
                        hlkQuanTri.Visible = false;
                        hlkDoanhNghiep.Visible = false;
                        if (CurrentContext.Cart != null)
                            CalculateCartItems();
                    }
                }
                Load_LoaiSanPham();
                DisplayLoginPanel();

            }
        }

        public void Load_LoaiSanPham()
        {
            LoaiSanPhamBUS BUS = new LoaiSanPhamBUS();
        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {

            TaiKhoanDAO DAO = new TaiKhoanDAO();
            u.TenTaiKhoan = txtTenDangNhap.Text;
            u.MatKhau = txtMatKhau.Text;
            if (DAO.CheckExist(u.TenTaiKhoan, u.MatKhau))
            {
                Response.Cookies["UserDangNhap"].Value = u.TenTaiKhoan;
                if (cbRemember.Checked)
                {
                    Response.Cookies["UserDangNhap"].Expires = DateTime.Now.AddDays(7);
                }
                u.InitCart();
                CurrentContext.TaiKhoanDTO = u;

                //string url = ViewState["RefUrl"] == null ? "~/Default.aspx" : ViewState["RefUrl"].ToString();
                Response.Redirect(DuongDan);
            }
        }

        protected void lbDangXuat_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Cookies["UserDangNhap"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect(Request.Url.ToString());
        }
        private void DisplayLoginPanel()
        {
            btnDangNhap.Visible = !CurrentContext.IsLogged;
            pnLogged.Visible = CurrentContext.IsLogged;
            TaiKhoanDAO DAO = new TaiKhoanDAO();
            if (CurrentContext.IsLogged)
            {
                if (Session["UserDangNhap"] == null)
                {
                    CurrentContext.TaiKhoanDTO = DAO.GetPassword(CurrentContext.UserName);
                    //CurrentContext.TaiKhoanDTO.InitCart();
                }
                lbInfo.Text = string.Format("Xin chào , <font color='gray'><b>{0}</b></font>", CurrentContext.UserName);
                //CalculateCartItems();
            }
        }
        public void CalculateCartItems()
        {
            lkCart.Text = string.Format("Giỏ hàng ({0})", CurrentContext.Cart.GetNumberOfItems());
        }

        public void XuLy_Menu()
        {
            lbtnTrangChu.ForeColor = System.Drawing.Color.FromName("White");
            lbtnMonAn.ForeColor = System.Drawing.Color.FromName("White");
            lbtnAo.ForeColor = System.Drawing.Color.FromName("White");
            lbtnQuan.ForeColor = System.Drawing.Color.FromName("White");
            lbtnDuLich.ForeColor = System.Drawing.Color.FromName("White");
            lbtnTinKhuyenMai.ForeColor = System.Drawing.Color.FromName("White");
            lbtnLienHe.ForeColor = System.Drawing.Color.FromName("White");

            if (Session["IDMenu"] == "lbtnTrangChu")
                lbtnTrangChu.ForeColor = System.Drawing.Color.FromName("Red");
            else if (Session["IDMenu"] == "lbtnMonAn")
                lbtnMonAn.ForeColor = System.Drawing.Color.FromName("Red");
            else if (Session["IDMenu"] == "lbtnAo")
                lbtnAo.ForeColor = System.Drawing.Color.FromName("Red");
            else if (Session["IDMenu"] == "lbtnQuan")
                lbtnQuan.ForeColor = System.Drawing.Color.FromName("Red");
            else if (Session["IDMenu"] == "lbtnDuLich")
                lbtnDuLich.ForeColor = System.Drawing.Color.FromName("Red");
            else if (Session["IDMenu"] == "lbtnTinKhuyenMai")
                lbtnTinKhuyenMai.ForeColor = System.Drawing.Color.FromName("Red");
            else if (Session["IDMenu"] == "lbtnLienHe")
                lbtnLienHe.ForeColor = System.Drawing.Color.FromName("Red");
        }

        protected void lbtnTrangChu_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnTrangChu";
            Response.Redirect("Default.aspx");
        }

        protected void lbtnMonAn_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnMonAn";
            Response.Redirect("ProductByCat.aspx?CatID=1");
        }

        protected void lbtnAo_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnAo";
            Response.Redirect("ProductByCat.aspx?CatID=3");
        }

        protected void lbtnQuan_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnQuan";
            Response.Redirect("ProductByCat.aspx?CatID=2");
        }

        protected void lbtnDuLich_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnDuLich";
            Response.Redirect("ProductByCat.aspx?CatID=4");
        }

        protected void lbtnTinKhuyenMai_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnTinKhuyenMai";
            Response.Redirect("TopDeal.aspx");
        }

        protected void lbtnLienHe_Click(object sender, EventArgs e)
        {
            Session["IDMenu"] = "lbtnLienHe";
            Response.Redirect("ContactUS.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        protected string GetDate()
        {
            if (Request.QueryString["proID"] != null)
            {
                int proID = int.Parse(Request.QueryString["proID"].ToString());
                TinKhuyenMaiDTO DTO = new TinKhuyenMaiBUS().GetFromId(proID);
                TimeSpan time = DTO.NgayHetHan - DateTime.Now;
                string a = time.ToString(@"dd\:hh\:mm\:ss");
                return a;
            }
            return null;
        }

    }
}